#!/bin/bash

## tomcat download directory
tomcat_source=$1

## tomcat install directory
tomcat_prefix=$2

if [ ! -d "${tomcat_source}" ]; then
        echo ">>Failed: tomcat_source directory is not exist, create it and execute. "
        exit 1
fi

if [ ! -d "${tomcat_prefix}" ]; then
        echo ">>Failed: directory of installing tomcat is not exist, create it and execute."
        exit 1
fi

tomcat_url="https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.53/bin/apache-tomcat-9.0.53.tar.gz"

cd ${tomcat_source} && wget ${tomcat_url}

if [ $? -eq "0" ]; then
        echo ">>Succeed: download tomcat successfully ..."
else
        echo ">>Failed: Tomcat download failed, installation interrupted "
        exit 4
fi

## 
tomcat_name=$(echo ${tomcat_url}|awk -F '/' '{print $NF}')
tar -xf ${tomcat_name} -C ${tomcat_prefix}

if [ $? -eq "0" ]; then
        echo ">>Succeed: Installation tomcat successfully ..."
else
        echo ">>Failed: Tomcat installation failed, installation interrupted "
        exit 4
fi

